//
//  OnLineTitleDetailVC.m
//  GC_OC
//
//  Created by lbh on 2022/4/8.
//

#import "OnLineTitleDetailVC.h"
#import "OnlineSubHeadView.h"
#import "OnLineSubMenuLIstVC.h"
@interface OnLineTitleDetailVC ()
@property (nonatomic,strong)OnlineSubHeadView *subHeadView;
@property (nonatomic, strong)ObjectAFNetWorking* objectAFNet;
@property (nonatomic,strong)UIView *navBarView;
@property (nonatomic,strong)NSDictionary *currenClassDict;
@end

@implementation OnLineTitleDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeadView];
    [self getTearchListData];
    [self setNeedsStatusBarAppearanceUpdate];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
//导航栏设置
-(void)creatHeadView{
    
    self.objectAFNet=[[ObjectAFNetWorking alloc]init];
    self.view.backgroundColor = UIColor.whiteColor;
    UIView* view=[[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,topHeight)];
    view.backgroundColor = AppMainColor;
    CGFloat titleHeight=[GC_OCTool getHeightForLabel:@"A" andFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)] andWidth:wid(100)];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(wid(65),StatusBarHeight+(topHeight-StatusBarHeight-titleHeight)/2.0,SCREEN_WIDTH-wid(65)*2,titleHeight)];
    titleLabel.textColor=[UIColor whiteColor];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:FontSize(17)]];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=self.vcTitle;
    titleLabel.text = self.vcTitle;
    [view addSubview:titleLabel];
    self.navBarView = view;
    
    UIButton* leftbtn=[UIButton new];
    leftbtn.frame=CGRectMake(wid(20),StatusBarHeight+(topHeight-StatusBarHeight-wid(40))/2.0,wid(40),wid(40));
    [leftbtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftbtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [leftbtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:leftbtn];
    [self.view addSubview:view];
}
//分页设置
- (void)createPageView{
    NSArray *titleArray = [self getTabHeadViewAllTitle];
    NSMutableArray *data = [[NSMutableArray alloc]init];
    for (NSString *str in titleArray) {
        NSDictionary *dict = @{WMZPageKeyName:str,WMZPageKeyTitleMarginY:@(-4)};
        [data addObject:dict];
    }
    //标题数组
    WMZPageParam *param = PageParam();
    param.wMenuImagePositionSet(PageBtnPositionLeft)
        .wMenuTitleColorSet(UIColor.blackColor)
        .wMenuTitleSelectColorSet(UIColor.blueColor)
        .wMenuTitleUIFontSet([UIFont boldSystemFontOfSize:16])
        .wMenuTitleSelectUIFontSet([UIFont boldSystemFontOfSize:16])
        .wTitleArrSet(data)
    .wMenuIndicatorColorSet(AppMainColor)
    .wMenuIndicatorWidthSet(30)
    .wMenuIndicatorRadioSet(2)
    .wMenuHeightSet(44)
    .wMenuIndicatorYSet(3)
//    .wMenuBgColorSet(UIColor.redColor)
//    WMZPageKeyTitleMarginY
    .wMenuBottomMarginYSet(-10)
    //控制器数组
        .wViewControllerSet(^UIViewController *(NSInteger index) {
            OnLineSubMenuLIstVC *vc = OnLineSubMenuLIstVC.new;
            vc.page = index;
            vc.classArray = [self getClassContentArray:index];
            vc.ID = self.ID;
            return (UIViewController*)vc;
        })
        .wMenuAnimalSet(PageTitleMenuAiQY)
        .wMenuTitleWidthSet(PageVCWidth/titleArray.count)
    //悬浮开启
        .wTopSuspensionSet(YES)
    //顶部可下拉
        .wBouncesSet(NO)
    //头视图y坐标从导航栏开始
        .wFromNaviSet(YES)
    //导航栏透明度变化
        .wNaviAlphaSet(NO)
    //下划线
        .wInsertMenuLineSet(^(UIView *bgView) {
            //修改内置线
            CGRect rect = bgView.frame;
            rect.size.height = 1;
            rect.origin.y -= 1;
            bgView.frame = rect;
            bgView.backgroundColor = RBG(244, 244, 244);
//            bgView.backgroundColor = UIColor.greenColor;
        })
    //头部
        .wMenuHeadViewSet(^UIView *{
            return self.subHeadView;
        })
    
    //导航栏标题透明度变化
        .wEventChildVCDidSrollSet(^(UIViewController *pageVC, CGPoint oldPoint, CGPoint newPonit, UIScrollView *currentScrollView) {
        });
    param.wEventEndTransferController = ^(UIViewController * _Nullable oldVC, UIViewController * _Nullable newVC, NSInteger oldIndex, NSInteger newIndex) {
    };
    self.param = param;
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navBarView.mas_bottom).offset(-20);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
//获取所有bottom字标题
- (NSArray *)getTabHeadViewAllTitle{
    NSMutableArray *titleArray = [[NSMutableArray alloc]init];
    id bottom = self.currenClassDict[@"data"][@"bottom"];
    if ([bottom isKindOfClass:[NSArray class]]) {
        NSArray *bottomArray = (NSArray *)bottom;
        for (NSDictionary *botdict in bottomArray) {
            NSString *nTitle = botdict[@"tile"];
            [titleArray addObject:nTitle];
        }
    }
    return titleArray;
}
///获取字典中content数组数据
- (NSArray *)getClassContentArray:(NSInteger)section{
    NSArray *classArray = [self getClassArrayData];
    NSDictionary *classDict = classArray[section];
    NSArray *content = classDict[@"content"];
    return content;
}
///组装所有的课程
- (NSArray *)getClassArrayData{
    NSMutableArray *DictArray = [[NSMutableArray alloc]init];
    id bottom = self.currenClassDict[@"data"][@"bottom"];
    if ([bottom isKindOfClass:[NSArray class]]) {
        NSArray *bottomArray = (NSArray *)bottom;
        for (NSDictionary *botdict in bottomArray) {
            [DictArray addObject:botdict];
        }
    }
    return DictArray;
}
-(void)leftBtnAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    if (self.navigationController.viewControllers.count == 1) {
        if ([ICMusicPlayManager sharedManager].narrowView) {
            [ICMusicPlayManager sharedManager].narrowView.frame = CGRectMake(0, SCREEN_HEIGHT - 50 - GCtabbarHeight, SCREEN_WIDTH, 50);
        }
    }
}
#pragma mark --NetWork
//更加课程ID获取教师列表
-(void)getTearchListData{
    [self.view showBusyHUD];
    NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/OnlineEducation_%@_getCateData_Json.json",self.ID]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSData* dataString=[NSData dataWithContentsOfFile:filePath];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        NSArray *teacher = dic[@"data"][@"teacher"];
        self.currenClassDict = dic;
        self.subHeadView.dataSource = teacher;
        [self createPageView];
        //保存数据
        [self.view hideBusyHUD];
        [GC_OCTool saveFileAction:filePath];
    }else{
        [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/home/getCateData?id=%@",server_addr,self.ID] finish:^(id  _Nonnull dataString) {
            [self.view hideBusyHUD];
            NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
            NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/OnlineEducation_%@_getCateData_Json.json",self.ID]];
            if ([fileManager fileExistsAtPath:filePath]) {
                [fileManager removeItemAtPath:filePath error:nil];
            }
            [dataString writeToFile:filePath atomically:YES];
            NSArray *teacher = dic[@"data"][@"teacher"];
            self.currenClassDict = dic;
            self.subHeadView.dataSource = teacher;
            [self createPageView];
        } andError:^(NSError * _Nonnull error) {
            [self.view hideBusyHUD];
        }];

    }
//    [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/home/getCateData?id=%@",server_addr,self.ID] finish:^(id  _Nonnull dataString) {
//        [self.view hideBusyHUD];
//        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
//        NSString *filePath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/OnlineEducation_%@_getCateData_Json.json",self.ID]];
//        if ([fileManager fileExistsAtPath:filePath]) {
//            [fileManager removeItemAtPath:filePath error:nil];
//        }
//        [dataString writeToFile:filePath atomically:YES];
//        NSArray *teacher = dic[@"data"][@"teacher"];
//        self.currenClassDict = dic;
//        self.subHeadView.dataSource = teacher;
//        [self createPageView];
//    } andError:^(NSError * _Nonnull error) {
//        [self.view hideBusyHUD];
//    }];
}
#pragma mark --lazy
- (OnlineSubHeadView *)subHeadView{
    if (!_subHeadView) {
        _subHeadView = [[OnlineSubHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 220)];
    }
    return _subHeadView;
}
@end

